/**
 * Created by no1 on 2015/5/18.
 */
'use strict';

var mysql = require('../common/mysql.js');
var M = require('./index.js');
var F = require('../common/function');
var mactStep = require('./mActivityStep');

// 用户地址信息

function marketActivity(actInfo) {
    this.set(actInfo);
}

marketActivity.prototype.get = function *(actid) {
    if (actid && actid > 0) { // 如果传此参数，则从DB中读取，否则返回本地数据
        let result = yield mysql.queryOne('SELECT ma.* FROM mh_activity ma where ma.actid=?', [actid]);
        if (result) {
            let stepResult = yield mysql.query('select * from mh_activity_step where actid=? order by sorder', [actid]);
            let stepArray = [];
            if (stepResult && stepResult.length > 0) {
                for (var step in stepResult) {
                    let stepEntity = new mactStep.activityStep(step);
                    stepArray.push(stepEntity);
                }
            }
            this.set({
                actid: actid,
                acttype: result['acttype'],
                acttitle: result['acttitle'],
                wareid: result['wareid'],
                begintime: result['begintime'],
                endtime: result['endtime'],
                detail: result['detail'],
                validbegintime: result['validbegintime'],
                validendtime: result['validendtime'],
                status: result['status'],
                imageurl: result['imageurl'],
                shareurl: result['shareurl'],
                steps: stepArray
            })
        } else {
            this.set({actid: 0});
        }
    }
    return this.actInfo;
};

marketActivity.prototype.set = function (actInfo) {
    this.actInfo = {
        actid: actInfo['actid'] || 0,
        acttype: actInfo['acttype'] || 0,
        acttitle: actInfo['acttitle'] || '',
        wareid: actInfo['wareid'] || 0,
        begintime: actInfo['begintime'] || null,
        endtime: actInfo['endtime'] || null,
        detail: actInfo['detail'] || '',
        validbegintime: actInfo['validbegintime'] || null,
        validendtime: actInfo['validendtime'] || null,
        status: actInfo['status'] || 0,
        imageurl: actInfo['imageurl'] || '',
        shareurl: actInfo['shareurl'] || '',
        steps: actInfo.steps || []
    };
    return M.returnData(0, '', this.actInfo);
};

marketActivity.prototype.delete = function *() {
    if (this.actInfo['actid'] && this.actInfo['actid'] > 0) {
        let result = yield mysql.query('update mh_activity set status=0 where actid=?', [this.actid]);
        if (result.affectedRows == 0) {
            return M.returnData(1, '删除失败');
        }
        else {
            return M.returnData(0, '删除成功');
        }
    } else {
        return M.returnData(1, '参数无效');
    }
};

marketActivity.prototype.save = function *() {
    if (this.aid > 0) { // 更新地址
        let result = yield mysql.query('UPDATE mh_user_address SET ' +
            ' province_id=?,' +
            ' province=?,' +
            ' city_id=?,' +
            ' city=?,' +
            ' district_id=?,' +
            ' district=?,' +
            ' street_id=?,' +
            ' street=?,' +
            ' address=?,' +
            ' username=?,' +
            ' mobile=?,' +
            ' postcode=?,' +
            ' status=?' +
            ' WHERE aid=? and uid=?', [
            this.province_id,
            this.province,
            this.city_id,
            this.city,
            this.district_id,
            this.district,
            this.street_id,
            this.street,
            this.address,
            this.username,
            this.mobile,
            this.postcode,
            this.status,
            this.aid,
            this.uid
        ]);
        if (result.affectedRows == 0) {
            return M.returnData(1, '更新失败，只能修改自己的收货地址');
        }
        else {
            return M.returnData(0, '更新成功');
        }
    } else { // 新地址
        let result = yield mysql.query('INSERT INTO mh_user_address(' +
            'uid,province_id,province,city_id,city,district_id,district,street_id,street,address,mobile,postcode,username,status' +
            ') VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,1)', [
            this.uid,
            this.province_id,
            this.province,
            this.city_id,
            this.city,
            this.district_id,
            this.district,
            this.street_id,
            this.street,
            this.address,
            this.mobile,
            this.postcode,
            this.username
        ]);
        if (result.affectedRows == 0) {
            return M.returnData(1, '保存失败');
        }
        else {
            this.actInfo['actid'] = result.insertId;
            return M.returnData(0, '保存成功');
        }
    }
};

/*
 @todo 判断活动开始时间及领取
 @return false 未开始，true 已经开始
 */
marketActivity.prototype.checkDate = function *(actTimeData) {
    let result = {};
    if (actTimeData['actid'] == 0) {
        result.retcode = 0;
    } else {
        let currentDate = F.timestamp();
        let actBeginTime = parseInt(new Date(new Date(actTimeData['begintime'] * 1000).format('yyyy-MM-dd') + ' 0:0:0 CST+800').getTime() / 1000);
        let actValidEndTime = parseInt(new Date(new Date(actTimeData['validendtime'] * 1000).format('yyyy-MM-dd') + ' 0:0:0 CST+800').getTime() / 1000) + 86400;
        if (currentDate < actBeginTime) {
            result.retcode = 1;
            result.retmsg = "活动即将开始";
        }
        else if (currentDate > actValidEndTime) {
            result.retcode = 2;
            result.retmsg = "活动已经结束";
        }
    }
    return M.returnData(0, '', result);
};

exports.marketActivity = marketActivity;